<!DOCTYPE html><html><head>
<title>pt::peg::container - Parser Tools</title>
<style type="text/css"><!--
    HTML {
	background: 	#FFFFFF;
	color: 		black;
    }
    BODY {
	background: 	#FFFFFF;
	color:	 	black;
    }
    DIV.doctools {
	margin-left:	10%;
	margin-right:	10%;
    }
    DIV.doctools H1,DIV.doctools H2 {
	margin-left:	-5%;
    }
    H1, H2, H3, H4 {
	margin-top: 	1em;
	font-family:	sans-serif;
	font-size:	large;
	color:		#005A9C;
	background: 	transparent;
	text-align:		left;
    }
    H1.doctools_title {
	text-align: center;
    }
    UL,OL {
	margin-right: 0em;
	margin-top: 3pt;
	margin-bottom: 3pt;
    }
    UL LI {
	list-style: disc;
    }
    OL LI {
	list-style: decimal;
    }
    DT {
	padding-top: 	1ex;
    }
    UL.doctools_toc,UL.doctools_toc UL, UL.doctools_toc UL UL {
	font:		normal 12pt/14pt sans-serif;
	list-style:	none;
    }
    LI.doctools_section, LI.doctools_subsection {
	list-style: 	none;
	margin-left: 	0em;
	text-indent:	0em;
	padding: 	0em;
    }
    PRE {
	display: 	block;
	font-family:	monospace;
	white-space:	pre;
	margin:		0%;
	padding-top:	0.5ex;
	padding-bottom:	0.5ex;
	padding-left:	1ex;
	padding-right:	1ex;
	width:		100%;
    }
    PRE.doctools_example {
	color: 		black;
	background: 	#f5dcb3;
	border:		1px solid black;
    }
    UL.doctools_requirements LI, UL.doctools_syntax LI {
	list-style: 	none;
	margin-left: 	0em;
	text-indent:	0em;
	padding:	0em;
    }
    DIV.doctools_synopsis {
	color: 		black;
	background: 	#80ffff;
	border:		1px solid black;
	font-family:	serif;
	margin-top: 	1em;
	margin-bottom: 	1em;
    }
    UL.doctools_syntax {
	margin-top: 	1em;
	border-top:	1px solid black;
    }
    UL.doctools_requirements {
	margin-bottom: 	1em;
	border-bottom:	1px solid black;
    }
--></style>
</head>
<!-- Generated from file 'pt_peg_container.man' by tcllib/doctools with format 'html'
   -->
<!-- Copyright &amp;copy; 2009 Andreas Kupries &amp;lt;andreas_kupries@users.sourceforge.net&amp;gt;
   -->
<!-- pt::peg::container.n
   -->
<body><hr> [
   <a href="../../../../../../../../home">Tcllib Home</a>
&#124; <a href="../../../../toc.html">Main Table Of Contents</a>
&#124; <a href="../../../toc.html">Table Of Contents</a>
&#124; <a href="../../../../index.html">Keyword Index</a>
&#124; <a href="../../../../toc0.html">Categories</a>
&#124; <a href="../../../../toc1.html">Modules</a>
&#124; <a href="../../../../toc2.html">Applications</a>
 ] <hr>
<div class="doctools">
<h1 class="doctools_title">pt::peg::container(n) 1 tcllib &quot;Parser Tools&quot;</h1>
<div id="name" class="doctools_section"><h2><a name="name">Name</a></h2>
<p>pt::peg::container - PEG Storage</p>
</div>
<div id="toc" class="doctools_section"><h2><a name="toc">Table Of Contents</a></h2>
<ul class="doctools_toc">
<li class="doctools_section"><a href="#toc">Table Of Contents</a></li>
<li class="doctools_section"><a href="#synopsis">Synopsis</a></li>
<li class="doctools_section"><a href="#section1">Description</a>
<ul>
<li class="doctools_subsection"><a href="#subsection1">Class API</a></li>
<li class="doctools_subsection"><a href="#subsection2">Object API</a></li>
</ul>
</li>
<li class="doctools_section"><a href="#section2">PEG serialization format</a>
<ul>
<li class="doctools_subsection"><a href="#subsection3">Example</a></li>
</ul>
</li>
<li class="doctools_section"><a href="#section3">PE serialization format</a>
<ul>
<li class="doctools_subsection"><a href="#subsection4">Example</a></li>
</ul>
</li>
<li class="doctools_section"><a href="#section4">Bugs, Ideas, Feedback</a></li>
<li class="doctools_section"><a href="#keywords">Keywords</a></li>
<li class="doctools_section"><a href="#category">Category</a></li>
<li class="doctools_section"><a href="#copyright">Copyright</a></li>
</ul>
</div>
<div id="synopsis" class="doctools_section"><h2><a name="synopsis">Synopsis</a></h2>
<div class="doctools_synopsis">
<ul class="doctools_requirements">
<li>package require <b class="pkgname">Tcl 8.5</b></li>
<li>package require <b class="pkgname">snit</b></li>
<li>package require <b class="pkgname">pt::peg::container <span class="opt">?1?</span></b></li>
</ul>
<ul class="doctools_syntax">
<li><a href="#1"><b class="cmd">::pt::peg</b> <i class="arg">objectName</i> <span class="opt">?<b class="const">=</b>|<b class="const">:=</b>|<b class="const">&lt;--</b>|<b class="const">as</b>|<b class="const">deserialize</b> <i class="arg">src</i>?</span></a></li>
<li><a href="#2"><i class="arg">objectName</i> <b class="method">destroy</b></a></li>
<li><a href="#3"><i class="arg">objectName</i> <b class="method">clear</b></a></li>
<li><a href="#4"><i class="arg">objectName</i> <b class="method">importer</b></a></li>
<li><a href="#5"><i class="arg">objectName</i> <b class="method">importer</b> <i class="arg">object</i></a></li>
<li><a href="#6"><i class="arg">objectName</i> <b class="method">exporter</b></a></li>
<li><a href="#7"><i class="arg">objectName</i> <b class="method">exporter</b> <i class="arg">object</i></a></li>
<li><a href="#8"><i class="arg">objectName</i> <b class="method">=</b> <i class="arg">source</i></a></li>
<li><a href="#9"><i class="arg">objectName</i> <b class="method">--&gt;</b> <i class="arg">destination</i></a></li>
<li><a href="#10"><i class="arg">objectName</i> <b class="method">serialize</b> <span class="opt">?<i class="arg">format</i>?</span></a></li>
<li><a href="#11"><i class="arg">objectName</i> <b class="method">deserialize =</b> <i class="arg">data</i> <span class="opt">?<i class="arg">format</i>?</span></a></li>
<li><a href="#12"><i class="arg">objectName</i> <b class="method">deserialize +=</b> <i class="arg">data</i> <span class="opt">?<i class="arg">format</i>?</span></a></li>
<li><a href="#13"><i class="arg">objectName</i> <b class="method">start</b></a></li>
<li><a href="#14"><i class="arg">objectName</i> <b class="method">start</b> <i class="arg">pe</i></a></li>
<li><a href="#15"><i class="arg">objectName</i> <b class="method">nonterminals</b></a></li>
<li><a href="#16"><i class="arg">objectName</i> <b class="method">modes</b></a></li>
<li><a href="#17"><i class="arg">objectName</i> <b class="method">modes</b> <i class="arg">dict</i></a></li>
<li><a href="#18"><i class="arg">objectName</i> <b class="method">rules</b></a></li>
<li><a href="#19"><i class="arg">objectName</i> <b class="method">rules</b> <i class="arg">dict</i></a></li>
<li><a href="#20"><i class="arg">objectName</i> <b class="method">add</b> <span class="opt">?<i class="arg">nt</i>...?</span></a></li>
<li><a href="#21"><i class="arg">objectName</i> <b class="method">remove</b> <span class="opt">?<i class="arg">nt</i>...?</span></a></li>
<li><a href="#22"><i class="arg">objectName</i> <b class="method">exists</b> <i class="arg">nt</i></a></li>
<li><a href="#23"><i class="arg">objectName</i> <b class="method">rename</b> <i class="arg">ntold</i> <i class="arg">ntnew</i></a></li>
<li><a href="#24"><i class="arg">objectName</i> <b class="method">mode</b> <i class="arg">nt</i></a></li>
<li><a href="#25"><i class="arg">objectName</i> <b class="method">mode</b> <i class="arg">nt</i> <i class="arg">mode</i></a></li>
<li><a href="#26"><i class="arg">objectName</i> <b class="method">rule</b> <i class="arg">nt</i></a></li>
<li><a href="#27"><i class="arg">objectName</i> <b class="method">rule</b> <i class="arg">nt</i> <i class="arg">pe</i></a></li>
</ul>
</div>
</div>
<div id="section1" class="doctools_section"><h2><a name="section1">Description</a></h2>
<p>Are you lost ?
Do you have trouble understanding this document ?
In that case please read the overview provided by the
<i class="term"><a href="pt_introduction.html">Introduction to Parser Tools</a></i>. This document is the
entrypoint to the whole system the current package is a part of.</p>
<p>This package provides a container class for parsing expression
grammars, with each instance storing a single grammar and allowing the
user to manipulate and query its definition.</p>
<p>It resides in the Storage section of the Core Layer of Parser Tools,
and is one of the three pillars the management of parsing expression
grammars resides on.</p>
<p><img alt="arch_core_container" src="../../../../image/arch_core_container.png"></p>
<p>The other two pillars are, as shown above</p>
<ol class="doctools_enumerated">
<li><p><i class="term"><a href="pt_peg_import.html">PEG Import</a></i>, and</p></li>
<li><p><i class="term"><a href="pt_peg_export.html">PEG Export</a></i></p></li>
</ol>
<p>Packages related to this are:</p>
<dl class="doctools_definitions">
<dt><b class="package"><a href="pt_rdengine.html">pt::rde</a></b></dt>
<dd><p>This package provides an implementation of PARAM, a virtual machine
for the parsing of a channel, geared towards the needs of handling
PEGs.</p></dd>
<dt><b class="package"><a href="pt_peg_interp.html">pt::peg::interp</a></b></dt>
<dd><p>This package implements an interpreter for PEGs on top of the virtual
machine provided by <b class="package">pt::peg::rde</b></p></dd>
</dl>
<div id="subsection1" class="doctools_subsection"><h3><a name="subsection1">Class API</a></h3>
<p>The package exports the API described here.</p>
<dl class="doctools_definitions">
<dt><a name="1"><b class="cmd">::pt::peg</b> <i class="arg">objectName</i> <span class="opt">?<b class="const">=</b>|<b class="const">:=</b>|<b class="const">&lt;--</b>|<b class="const">as</b>|<b class="const">deserialize</b> <i class="arg">src</i>?</span></a></dt>
<dd><p>The command creates a new container object for a parsing expression
grammar and returns the fully qualified name of the object command as
its result. The API of this object command is described in the section
<span class="sectref"><a href="#subsection2">Object API</a></span>. It may be used to invoke various operations on
the object.</p>
<p>The new container will be empty if no <i class="arg">src</i> is specified. Otherwise
it will contain a copy of the grammar contained in the <i class="arg">src</i>.
All operators except <b class="const">deserialize</b> interpret <i class="arg">src</i> as a
container object command. The <b class="const">deserialize</b> operator interprets
<i class="arg">src</i> as the serialization of a parsing expression grammar
instead, as specified in section <span class="sectref"><a href="#section2">PEG serialization format</a></span>.</p>
<p>An empty grammar has no nonterminal symbols, and the start expression
is the empty expression, i.e. epsilon. It is <i class="term">valid</i>, but not
<i class="term">useful</i>.</p></dd>
</dl>
</div>
<div id="subsection2" class="doctools_subsection"><h3><a name="subsection2">Object API</a></h3>
<p>All objects created by this package provide the following methods for
the manipulation and querying of their contents:</p>
<dl class="doctools_definitions">
<dt><a name="2"><i class="arg">objectName</i> <b class="method">destroy</b></a></dt>
<dd><p>This method destroys the object, releasing all claimed memory, and
deleting the associated object command.</p></dd>
<dt><a name="3"><i class="arg">objectName</i> <b class="method">clear</b></a></dt>
<dd><p>This method resets the object to contain the empty grammar. It does
<em>not</em> destroy the object itself.</p></dd>
<dt><a name="4"><i class="arg">objectName</i> <b class="method">importer</b></a></dt>
<dd><p>This method returns the import manager object currently attached to
the container, if any.</p></dd>
<dt><a name="5"><i class="arg">objectName</i> <b class="method">importer</b> <i class="arg">object</i></a></dt>
<dd><p>This method attaches the <i class="arg">object</i> as import manager to the
container, and returns it as the result of the command.
Note that the <i class="arg">object</i> is <em>not</em> put into ownership of the
container. I.e., destruction of the container will <em>not</em> destroy
the <i class="arg">object</i>.</p>
<p>It is expected that <i class="arg">object</i> provides a method named
<b class="method">import text</b> which takes a text and a format name, and
returns the canonical serialization of the table of contents contained in
the text, assuming the given format.</p></dd>
<dt><a name="6"><i class="arg">objectName</i> <b class="method">exporter</b></a></dt>
<dd><p>This method returns the export manager object currently attached to
the container, if any.</p></dd>
<dt><a name="7"><i class="arg">objectName</i> <b class="method">exporter</b> <i class="arg">object</i></a></dt>
<dd><p>This method attaches the <i class="arg">object</i> as export manager to the
container, and returns it as the result of the command.
Note that the <i class="arg">object</i> is <em>not</em> put into ownership of the
container. I.e., destruction of the container will <em>not</em> destroy
the <i class="arg">object</i>.</p>
<p>It is expected that <i class="arg">object</i> provides a method named
<b class="method">export object</b> which takes the container and a format name,
and returns a text encoding table of contents stored in the container, in
the given format. It is further expected that the <i class="arg">object</i> will
use the container's method <b class="method">serialize</b> to obtain the
serialization of the table of contents from which to generate the text.</p></dd>
<dt><a name="8"><i class="arg">objectName</i> <b class="method">=</b> <i class="arg">source</i></a></dt>
<dd><p>This method assigns the contents of the PEG object <i class="arg">source</i> to
ourselves, overwriting the existing definition. This is the assignment
operator for grammars.</p>
<p>This operation is in effect equivalent to</p>
<pre class="doctools_example">
    <i class="arg">objectName</i> <b class="method">deserialize =</b> [<i class="arg">source</i> <b class="method">serialize</b>]
</pre>
</dd>
<dt><a name="9"><i class="arg">objectName</i> <b class="method">--&gt;</b> <i class="arg">destination</i></a></dt>
<dd><p>This method assigns our contents to the PEG object <i class="arg">destination</i>,
overwriting the existing definition. This is the reverse assignment
operator for grammars.</p>
<p>This operation is in effect equivalent to</p>
<pre class="doctools_example">
    <i class="arg">destination</i> <b class="method">deserialize =</b> [<i class="arg">objectName</i> <b class="method">serialize</b>]
</pre>
</dd>
<dt><a name="10"><i class="arg">objectName</i> <b class="method">serialize</b> <span class="opt">?<i class="arg">format</i>?</span></a></dt>
<dd><p>This method returns our grammar in some textual form usable for
transfer, persistent storage, etc. If no <i class="arg">format</i> is not specified
the returned result is the canonical serialization of the grammar, as
specified in the section <span class="sectref"><a href="#section2">PEG serialization format</a></span>.</p>
<p>Otherwise the object will use the attached export manager to convert
the data to the specified format. In that case the method will fail
with an error if the container has no export manager attached to it.</p></dd>
<dt><a name="11"><i class="arg">objectName</i> <b class="method">deserialize =</b> <i class="arg">data</i> <span class="opt">?<i class="arg">format</i>?</span></a></dt>
<dd><p>This is the complementary method to <b class="method">serialize</b>.
It replaces the current definition with the grammar contained in the
<i class="arg">data</i>. If no <i class="arg">format</i> was specified it is assumed to be the
regular serialization of a grammar, as specified in the section
<span class="sectref"><a href="#section2">PEG serialization format</a></span></p>
<p>Otherwise the object will use the attached import manager to convert
the data from the specified format to a serialization it can handle.
In that case the method will fail with an error if the container has
no import manager attached to it.</p>
<p>The result of the method is the empty string.</p></dd>
<dt><a name="12"><i class="arg">objectName</i> <b class="method">deserialize +=</b> <i class="arg">data</i> <span class="opt">?<i class="arg">format</i>?</span></a></dt>
<dd><p>This method behaves like <b class="method">deserialize =</b> in its essentials,
except that it merges the grammar in the <i class="arg">data</i> to its
contents instead of replacing it.
The method will fail with an error and leave the grammar unchanged if
merging is not possible, i.e. would produce an invalid grammar.</p>
<p>The result of the method is the empty string.</p></dd>
<dt><a name="13"><i class="arg">objectName</i> <b class="method">start</b></a></dt>
<dd><p>This method returns the current start expression of the grammar.</p></dd>
<dt><a name="14"><i class="arg">objectName</i> <b class="method">start</b> <i class="arg">pe</i></a></dt>
<dd><p>This method defines the <i class="term">start expression</i> of the grammar. It
replaces the current start expression with the parsing expression
<i class="arg">pe</i>, and returns the new start expression.</p>
<p>The method will fail with an error and leave the grammar unchanged if
<i class="arg">pe</i> does not contain a valid parsing expression as specified in
the section <span class="sectref"><a href="#section3">PE serialization format</a></span>.</p></dd>
<dt><a name="15"><i class="arg">objectName</i> <b class="method">nonterminals</b></a></dt>
<dd><p>This method returns the set of all nonterminal symbols known to the
grammar.</p></dd>
<dt><a name="16"><i class="arg">objectName</i> <b class="method">modes</b></a></dt>
<dd><p>This method returns a dictionary mapping the set of all nonterminal
symbols known to the grammar to their semantic modes.</p></dd>
<dt><a name="17"><i class="arg">objectName</i> <b class="method">modes</b> <i class="arg">dict</i></a></dt>
<dd><p>This method takes a dictionary mapping a set of nonterminal symbols
known to the grammar to their semantic modes, and returns the new full
mapping of nonterminal symbols to semantic modes.</p>
<p>The method will fail with an error if any of the nonterminal symbols
in the dictionary is not known to the grammar, or the empty string,
i.e. an invalid nonterminal symbol, or if any the chosen <i class="arg">mode</i>s
is not one of the legal values.</p></dd>
<dt><a name="18"><i class="arg">objectName</i> <b class="method">rules</b></a></dt>
<dd><p>This method returns a dictionary mapping the set of all nonterminal
symbols known to the grammar to their parsing expressions (right-hand
sides).</p></dd>
<dt><a name="19"><i class="arg">objectName</i> <b class="method">rules</b> <i class="arg">dict</i></a></dt>
<dd><p>This method takes a dictionary mapping a set of nonterminal symbols
known to the grammar to their parsing expressions (right-hand sides),
and returns the new full mapping of nonterminal symbols to parsing
expressions.</p>
<p>The method will fail with an error any of the nonterminal symbols in
the dictionary is not known to the grammar, or the empty string,
i.e. an invalid nonterminal symbol, or any of the chosen parsing
expressions is not a valid parsing expression as specified in the
section <span class="sectref"><a href="#section3">PE serialization format</a></span>.</p></dd>
<dt><a name="20"><i class="arg">objectName</i> <b class="method">add</b> <span class="opt">?<i class="arg">nt</i>...?</span></a></dt>
<dd><p>This method adds the nonterminal symbols <i class="arg">nt</i>, etc. to the
grammar, and defines default semantic mode and expression for it
(<b class="const">value</b> and <b class="const">epsilon</b> respectively).
The method returns the empty string as its result.</p>
<p>The method will fail with an error and leaves the grammar unchanged if
any of the nonterminal symbols are either already defined in our
grammar, or are the empty string (an invalid nonterminal symbol).</p>
<p>The method does nothing if no symbol was specified as argument.</p></dd>
<dt><a name="21"><i class="arg">objectName</i> <b class="method">remove</b> <span class="opt">?<i class="arg">nt</i>...?</span></a></dt>
<dd><p>This method removes the named nonterminal symbols <i class="arg">nt</i>, etc. from
the set of nonterminal symbols known to our grammar.
The method returns the empty string as its result.</p>
<p>The method will fail with an error and leave the grammar unchanged if
any of the nonterminal symbols is not known to the grammar, or is the
empty string, i.e. an invalid nonterminal symbol.</p></dd>
<dt><a name="22"><i class="arg">objectName</i> <b class="method">exists</b> <i class="arg">nt</i></a></dt>
<dd><p>This method tests whether the nonterminal symbol <i class="arg">nt</i> is known
to our grammar or not.
The result is a boolean value. It will be set to <b class="const">true</b> if
<i class="arg">nt</i> is known, and <b class="const">false</b> otherwise.</p>
<p>The method will fail with an error if <i class="arg">nt</i> is the empty string,
i.e. an invalid nonterminal symbol.</p></dd>
<dt><a name="23"><i class="arg">objectName</i> <b class="method">rename</b> <i class="arg">ntold</i> <i class="arg">ntnew</i></a></dt>
<dd><p>This method renames the nonterminal symbol <i class="arg">ntold</i> to <i class="arg">ntnew</i>.
The method returns the empty string as its result.</p>
<p>The method will fail with an error and leave the grammar unchanged if
either <i class="arg">ntold</i> is not known to the grammar, or <i class="arg">ntnew</i> is
already known, or any of them is the empty string, i.e. an invalid
nonterminal symbol.</p></dd>
<dt><a name="24"><i class="arg">objectName</i> <b class="method">mode</b> <i class="arg">nt</i></a></dt>
<dd><p>This method returns the current semantic mode for the nonterminal
symbol <i class="arg">nt</i>.</p>
<p>The method will fail with an error if <i class="arg">nt</i> is not known to the
grammar, or the empty string, i.e. an invalid nonterminal symbol.</p></dd>
<dt><a name="25"><i class="arg">objectName</i> <b class="method">mode</b> <i class="arg">nt</i> <i class="arg">mode</i></a></dt>
<dd><p>This mode sets the semantic mode for the nonterminal symbol <i class="arg">nt</i>,
and returns the new mode.
The method will fail with an error if <i class="arg">nt</i> is not known to the
grammar, or the empty string, i.e. an invalid nonterminal symbol, or
the chosen <i class="arg">mode</i> is not one of the legal values.</p>
<p>The following modes are legal:</p>
<dl class="doctools_definitions">
<dt><b class="const">value</b></dt>
<dd><p>The semantic value of the nonterminal symbol is an abstract syntax
tree consisting of a single node node for the nonterminal itself,
which has the ASTs of the symbol's right hand side as its children.</p></dd>
<dt><b class="const">leaf</b></dt>
<dd><p>The semantic value of the nonterminal symbol is an abstract syntax
tree consisting of a single node node for the nonterminal, without any
children. Any ASTs generated by the symbol's right hand side are
discarded.</p></dd>
<dt><b class="const">void</b></dt>
<dd><p>The nonterminal has no semantic value. Any ASTs generated by the
symbol's right hand side are discarded (as well).</p></dd>
</dl></dd>
<dt><a name="26"><i class="arg">objectName</i> <b class="method">rule</b> <i class="arg">nt</i></a></dt>
<dd><p>This method returns the current parsing expression (right-hand side)
for the nonterminal symbol <i class="arg">nt</i>.</p>
<p>The method will fail with an error if <i class="arg">nt</i> is not known to the
grammar, or the empty string, i.e. an invalid nonterminal symbol.</p></dd>
<dt><a name="27"><i class="arg">objectName</i> <b class="method">rule</b> <i class="arg">nt</i> <i class="arg">pe</i></a></dt>
<dd><p>This method set the parsing expression (right-hand side) of the
nonterminal <i class="arg">nt</i> to <i class="arg">pe</i>, and returns the new parsing
expression.</p>
<p>The method will fail with an error if <i class="arg">nt</i> is not known to the
grammar, or the empty string, i.e. an invalid nonterminal symbol, or
<i class="arg">pe</i> does not contain a valid parsing expression as specified in
the section <span class="sectref"><a href="#section3">PE serialization format</a></span>.</p></dd>
</dl>
</div>
</div>
<div id="section2" class="doctools_section"><h2><a name="section2">PEG serialization format</a></h2>
<p>Here we specify the format used by the Parser Tools to serialize
Parsing Expression Grammars as immutable values for transport,
comparison, etc.</p>
<p>We distinguish between <i class="term">regular</i> and <i class="term">canonical</i>
serializations.
While a PEG may have more than one regular serialization only exactly
one of them will be <i class="term">canonical</i>.</p>
<dl class="doctools_definitions">
<dt>regular serialization</dt>
<dd><ol class="doctools_enumerated">
<li><p>The serialization of any PEG is a nested Tcl dictionary.</p></li>
<li><p>This dictionary holds a single key, <b class="const">pt::grammar::peg</b>, and its
value. This value holds the contents of the grammar.</p></li>
<li><p>The contents of the grammar are a Tcl dictionary holding the set of
nonterminal symbols and the starting expression. The relevant keys and
their values are</p>
<dl class="doctools_definitions">
<dt><b class="const">rules</b></dt>
<dd><p>The value is a Tcl dictionary whose keys are the names of the
nonterminal symbols known to the grammar.</p>
<ol class="doctools_enumerated">
<li><p>Each nonterminal symbol may occur only once.</p></li>
<li><p>The empty string is not a legal nonterminal symbol.</p></li>
<li><p>The value for each symbol is a Tcl dictionary itself. The relevant
keys and their values in this dictionary are</p>
<dl class="doctools_definitions">
<dt><b class="const">is</b></dt>
<dd><p>The value is the serialization of the parsing expression describing
the symbols sentennial structure, as specified in the section
<span class="sectref"><a href="#section3">PE serialization format</a></span>.</p></dd>
<dt><b class="const">mode</b></dt>
<dd><p>The value can be one of three values specifying how a parser should
handle the semantic value produced by the symbol.</p>
<dl class="doctools_definitions">
<dt><b class="const">value</b></dt>
<dd><p>The semantic value of the nonterminal symbol is an abstract syntax
tree consisting of a single node node for the nonterminal itself,
which has the ASTs of the symbol's right hand side as its children.</p></dd>
<dt><b class="const">leaf</b></dt>
<dd><p>The semantic value of the nonterminal symbol is an abstract syntax
tree consisting of a single node node for the nonterminal, without any
children. Any ASTs generated by the symbol's right hand side are
discarded.</p></dd>
<dt><b class="const">void</b></dt>
<dd><p>The nonterminal has no semantic value. Any ASTs generated by the
symbol's right hand side are discarded (as well).</p></dd>
</dl></dd>
</dl>
</li>
</ol></dd>
<dt><b class="const">start</b></dt>
<dd><p>The value is the serialization of the start parsing expression of the
grammar, as specified in the section <span class="sectref"><a href="#section3">PE serialization format</a></span>.</p></dd>
</dl>
</li>
<li><p>The terminal symbols of the grammar are specified implicitly as the
set of all terminal symbols used in the start expression and on the
RHS of the grammar rules.</p></li>
</ol></dd>
<dt>canonical serialization</dt>
<dd><p>The canonical serialization of a grammar has the format as specified
in the previous item, and then additionally satisfies the constraints
below, which make it unique among all the possible serializations of
this grammar.</p>
<ol class="doctools_enumerated">
<li><p>The keys found in all the nested Tcl dictionaries are sorted in
ascending dictionary order, as generated by Tcl's builtin command
<b class="cmd">lsort -increasing -dict</b>.</p></li>
<li><p>The string representation of the value is the canonical representation
of a Tcl dictionary. I.e. it does not contain superfluous whitespace.</p></li>
</ol></dd>
</dl>
<div id="subsection3" class="doctools_subsection"><h3><a name="subsection3">Example</a></h3>
<p>Assuming the following PEG for simple mathematical expressions</p>
<pre class="doctools_example">
PEG calculator (Expression)
    Digit      &lt;- '0'/'1'/'2'/'3'/'4'/'5'/'6'/'7'/'8'/'9'       ;
    Sign       &lt;- '-' / '+'                                     ;
    Number     &lt;- Sign? Digit+                                  ;
    Expression &lt;- Term (AddOp Term)*                            ;
    MulOp      &lt;- '*' / '/'                                     ;
    Term       &lt;- Factor (MulOp Factor)*                        ;
    AddOp      &lt;- '+'/'-'                                       ;
    Factor     &lt;- '(' Expression ')' / Number                   ;
END;
</pre>
<p>then its canonical serialization (except for whitespace) is</p>
<pre class="doctools_example">
pt::grammar::peg {
    rules {
        AddOp      {is {/ {t -} {t +}}                                                                mode value}
        Digit      {is {/ {t 0} {t 1} {t 2} {t 3} {t 4} {t 5} {t 6} {t 7} {t 8} {t 9}}                mode value}
        Expression {is {x {n Term} {* {x {n AddOp} {n Term}}}}                                        mode value}
        Factor     {is {/ {x {t (} {n Expression} {t )}} {n Number}}                                  mode value}
        MulOp      {is {/ {t *} {t /}}                                                                mode value}
        Number     {is {x {? {n Sign}} {+ {n Digit}}}                                                 mode value}
        Sign       {is {/ {t -} {t +}}                                                                mode value}
        Term       {is {x {n Factor} {* {x {n MulOp} {n Factor}}}}                                    mode value}
    }
    start {n Expression}
}
</pre>
</div>
</div>
<div id="section3" class="doctools_section"><h2><a name="section3">PE serialization format</a></h2>
<p>Here we specify the format used by the Parser Tools to serialize
Parsing Expressions as immutable values for transport, comparison,
etc.</p>
<p>We distinguish between <i class="term">regular</i> and <i class="term">canonical</i>
serializations.
While a parsing expression may have more than one regular
serialization only exactly one of them will be <i class="term">canonical</i>.</p>
<dl class="doctools_definitions">
<dt>Regular serialization</dt>
<dd><dl class="doctools_definitions">
<dt><b class="const">Atomic Parsing Expressions</b></dt>
<dd><ol class="doctools_enumerated">
<li><p>The string <b class="const">epsilon</b> is an atomic parsing expression. It matches
the empty string.</p></li>
<li><p>The string <b class="const">dot</b> is an atomic parsing expression. It matches
any character.</p></li>
<li><p>The string <b class="const">alnum</b> is an atomic parsing expression. It matches
any Unicode alphabet or digit character. This is a custom extension of
PEs based on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">alpha</b> is an atomic parsing expression. It matches
any Unicode alphabet character. This is a custom extension of PEs
based on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">ascii</b> is an atomic parsing expression. It matches
any Unicode character below U0080. This is a custom extension of PEs
based on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">control</b> is an atomic parsing expression. It matches
any Unicode control character. This is a custom extension of PEs based
on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">digit</b> is an atomic parsing expression. It matches
any Unicode digit character. Note that this includes characters
outside of the [0..9] range. This is a custom extension of PEs
based on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">graph</b> is an atomic parsing expression. It matches
any Unicode printing character, except for space. This is a custom
extension of PEs based on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">lower</b> is an atomic parsing expression. It matches
any Unicode lower-case alphabet character. This is a custom extension
of PEs based on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">print</b> is an atomic parsing expression. It matches
any Unicode printing character, including space. This is a custom
extension of PEs based on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">punct</b> is an atomic parsing expression. It matches
any Unicode punctuation character. This is a custom extension of PEs
based on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">space</b> is an atomic parsing expression. It matches
any Unicode space character. This is a custom extension of PEs based
on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">upper</b> is an atomic parsing expression. It matches
any Unicode upper-case alphabet character. This is a custom extension
of PEs based on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">wordchar</b> is an atomic parsing expression. It
matches any Unicode word character. This is any alphanumeric character
(see alnum), and any connector punctuation characters (e.g.
underscore). This is a custom extension of PEs based on Tcl's builtin
command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">xdigit</b> is an atomic parsing expression. It matches
any hexadecimal digit character. This is a custom extension of PEs
based on Tcl's builtin command <b class="cmd">string is</b>.</p></li>
<li><p>The string <b class="const">ddigit</b> is an atomic parsing expression. It matches
any decimal digit character. This is a custom extension of PEs based
on Tcl's builtin command <b class="cmd">regexp</b>.</p></li>
<li><p>The expression
    [list t <b class="variable">x</b>]
is an atomic parsing expression. It matches the terminal string <b class="variable">x</b>.</p></li>
<li><p>The expression
    [list n <b class="variable">A</b>]
is an atomic parsing expression. It matches the nonterminal <b class="variable">A</b>.</p></li>
</ol></dd>
<dt><b class="const">Combined Parsing Expressions</b></dt>
<dd><ol class="doctools_enumerated">
<li><p>For parsing expressions <b class="variable">e1</b>, <b class="variable">e2</b>, ... the result of
    [list / <b class="variable">e1</b> <b class="variable">e2</b> ... ]
is a parsing expression as well.
This is the <i class="term">ordered choice</i>, aka <i class="term">prioritized choice</i>.</p></li>
<li><p>For parsing expressions <b class="variable">e1</b>, <b class="variable">e2</b>, ... the result of
    [list x <b class="variable">e1</b> <b class="variable">e2</b> ... ]
is a parsing expression as well.
This is the <i class="term">sequence</i>.</p></li>
<li><p>For a parsing expression <b class="variable">e</b> the result of
    [list * <b class="variable">e</b>]
is a parsing expression as well.
This is the <i class="term">kleene closure</i>, describing zero or more
repetitions.</p></li>
<li><p>For a parsing expression <b class="variable">e</b> the result of
    [list + <b class="variable">e</b>]
is a parsing expression as well.
This is the <i class="term">positive kleene closure</i>, describing one or more
repetitions.</p></li>
<li><p>For a parsing expression <b class="variable">e</b> the result of
    [list &amp; <b class="variable">e</b>]
is a parsing expression as well.
This is the <i class="term">and lookahead predicate</i>.</p></li>
<li><p>For a parsing expression <b class="variable">e</b> the result of
    [list ! <b class="variable">e</b>]
is a parsing expression as well.
This is the <i class="term">not lookahead predicate</i>.</p></li>
<li><p>For a parsing expression <b class="variable">e</b> the result of
    [list ? <b class="variable">e</b>]
is a parsing expression as well.
This is the <i class="term">optional input</i>.</p></li>
</ol></dd>
</dl></dd>
<dt>Canonical serialization</dt>
<dd><p>The canonical serialization of a parsing expression has the format as
specified in the previous item, and then additionally satisfies the
constraints below, which make it unique among all the possible
serializations of this parsing expression.</p>
<ol class="doctools_enumerated">
<li><p>The string representation of the value is the canonical representation
of a pure Tcl list. I.e. it does not contain superfluous whitespace.</p></li>
<li><p>Terminals are <em>not</em> encoded as ranges (where start and end of the
range are identical).</p></li>
</ol></dd>
</dl>
<div id="subsection4" class="doctools_subsection"><h3><a name="subsection4">Example</a></h3>
<p>Assuming the parsing expression shown on the right-hand side of the
rule</p>
<pre class="doctools_example">
    Expression &lt;- Term (AddOp Term)*
</pre>
<p>then its canonical serialization (except for whitespace) is</p>
<pre class="doctools_example">
    {x {n Term} {* {x {n AddOp} {n Term}}}}
</pre>
</div>
</div>
<div id="section4" class="doctools_section"><h2><a name="section4">Bugs, Ideas, Feedback</a></h2>
<p>This document, and the package it describes, will undoubtedly contain
bugs and other problems.
Please report such in the category <em>pt</em> of the
<a href="http://core.tcl.tk/tcllib/reportlist">Tcllib Trackers</a>.
Please also report any ideas for enhancements you may have for either
package and/or documentation.</p>
<p>When proposing code changes, please provide <em>unified diffs</em>,
i.e the output of <b class="const">diff -u</b>.</p>
<p>Note further that <em>attachments</em> are strongly preferred over
inlined patches. Attachments can be made by going to the <b class="const">Edit</b>
form of the ticket immediately after its creation, and then using the
left-most button in the secondary navigation bar.</p>
</div>
<div id="keywords" class="doctools_section"><h2><a name="keywords">Keywords</a></h2>
<p><a href="../../../../index.html#ebnf">EBNF</a>, <a href="../../../../index.html#ll_k_">LL(k)</a>, <a href="../../../../index.html#peg">PEG</a>, <a href="../../../../index.html#tdpl">TDPL</a>, <a href="../../../../index.html#context_free_languages">context-free languages</a>, <a href="../../../../index.html#expression">expression</a>, <a href="../../../../index.html#grammar">grammar</a>, <a href="../../../../index.html#matching">matching</a>, <a href="../../../../index.html#parser">parser</a>, <a href="../../../../index.html#parsing_expression">parsing expression</a>, <a href="../../../../index.html#parsing_expression_grammar">parsing expression grammar</a>, <a href="../../../../index.html#push_down_automaton">push down automaton</a>, <a href="../../../../index.html#recursive_descent">recursive descent</a>, <a href="../../../../index.html#state">state</a>, <a href="../../../../index.html#top_down_parsing_languages">top-down parsing languages</a>, <a href="../../../../index.html#transducer">transducer</a></p>
</div>
<div id="category" class="doctools_section"><h2><a name="category">Category</a></h2>
<p>Parsing and Grammars</p>
</div>
<div id="copyright" class="doctools_section"><h2><a name="copyright">Copyright</a></h2>
<p>Copyright &copy; 2009 Andreas Kupries &lt;andreas_kupries@users.sourceforge.net&gt;</p>
</div>
</div></body></html>
